<?
include "connectDB.php";


//echo mysql_num_rows($result1);
$query2 = 'select concept from relations';
$result2 = mysql_query($query2) or die(mysql_error());
$total_concepts = mysql_num_rows($result2);
$m=0;
$c=0;
$concepts;
$matched;
$topic;
$g=0;
	while($categories = mysql_fetch_array($result2)){
		$concept = strtolower(str_replace('_',' ',$categories['concept']));
		//echo $concept.'<br>';
		//if(trim($post) != null){
		//if(substr_count($post, $concept)) $matches++;;
		//echo substr_count($post, 'linux');
		$concepts[$g++]=$concept;
		$c++;	
	}
	$v=0;
	$query1 = 'select * from so_posts';
	$result1 = mysql_query($query1) or die(mysql_error());
	$tot = mysql_num_rows($result1);
	while($posts = mysql_fetch_array($result1)){
		$post = $posts['Body'];
		//echo $post.'<br>';
		$t = $posts['Topic'];
		$matches=0;
		foreach($concepts as $c){
			if(substr_count($post, $c)) {
				$matches++;
				$matched[$c]++;
				$topic[$t]++;
			}
		}
		if($matches>0) $v++;
	}
$tot_concept = count($matched);
//print_r($topic);
arsort($matched);
echo 'Total Stackoverflow Posts with atleast 1 match '.$v.' out of '.$tot.'<br>';
echo "Total wikipedia concepts that have matched: ".$tot_concept.'out of '.$total_concepts. ' which is '. $tot_concept*100/$total_concepts.'%<br>';
echo "<table><tr><th>Concept</th><th>No of articles</th></tr>";

foreach($matched as $a=>$b){
	echo '<tr><td>'.$a.'</td><td> '.$b.'</td></tr>';
}

echo '</table>';
echo '<table><tr><th>Folder name</th><th>% of posts with atleast 1 concept</th><th>Total Posts</th></tr>';
//arsort($topic);
foreach($topic as $t=>$v){
	$query4 = "select * from so_posts where Topic='$t'";
	$result4 = mysql_query($query4) or die(mysql_error());
	$c = mysql_num_rows($result4);
	echo '<tr><td>'.$t.'</td><td>'.$v*100/$c.'</td><td>'.$c.'</td></tr>';
}
echo '</table>';
?>